package org.xukai.core.aop.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author xukai
 * @desc
 * @date 2016-12-06-下午 7:41
 */
public class CGLibProxy implements MethodInterceptor {

    private static CGLibProxy proxy = new CGLibProxy();

    public static CGLibProxy newInstance(){
        return proxy;
    }


    public <T> T getProxy(Class<T> cls){

        return (T) Enhancer.create(cls,this);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println(methodProxy.getSuperName());
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println(method.getName());
//        Object invoke = method.invoke(o, objects);
        System.out.println(result + "    " );
        return result;
    }
}
